package com.hzjq.encrypt.ui

import android.content.Context
import android.content.Intent
import com.hzjq.encrypt.adapter.QbqAdapter
import com.hzjq.encrypt.bean.EnBean
import com.hzjq.encrypt.bean.Sbbhs
import com.hzjq.encrypt.pop.QbqEditorPop

class QbqActivity : AbsListActivity<Sbbhs>() {


    companion object {
        fun start(context: Context, data: EnBean?) {
            val intent = Intent(context, QbqActivity::class.java)
            if (data == null) {
                intent.putExtra("data", EnBean())
            } else {
                intent.putExtra("data", data)
            }
            context.startActivity(intent)
        }

    }

    private val mQbqAdapter = QbqAdapter(arrayListOf())

    override fun onDataCreated() {
        mAdapter = mQbqAdapter
        if (data.sbbhs == null) {
            data.sbbhs = arrayListOf()
        }

        mQbqAdapter.setNewInstance(data.sbbhs)

        setShowTitle("起爆器(${data.sbbhs.size})台")
    }

    override fun onRemoveItemClick(position: Int) {
        data.sbbhs.removeAt(position)
        setShowTitle("起爆器(${data.sbbhs.size})台")
        notifyDataChanged()
    }

    override fun onItemEditorClick(position: Int) {
        QbqEditorPop.show(this,object : QbqEditorPop.OnQbqEditorCallback{
            override fun onQbqEditor(sbbhs: Sbbhs) {
                data.sbbhs[sbbhs.position] = sbbhs
                mQbqAdapter.notifyDataSetChanged()
                notifyDataChanged()
            }
        }, mQbqAdapter.getItem(position))
    }

    override fun onAddNewClick() {
        QbqEditorPop.show(this,object : QbqEditorPop.OnQbqEditorCallback{
            override fun onQbqEditor(sbbhs: Sbbhs) {
                data.sbbhs.add(0, sbbhs)
                setShowTitle("起爆器(${data.sbbhs.size})台")
                notifyDataChanged()
            }
        }, Sbbhs())
    }
}